package org.example.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.Dto.LearnHistoryDTO;
import org.example.Entity.LearnHistory;
import org.example.Entity.Word;
import org.example.Mapper.LearnHistoryMapper;
import org.example.Service.LearnHistoryService;
import org.example.Utils.HttpUtil;
import org.example.Vo.LearnHistoryLineVO;
import org.example.Vo.LearnHistoryPieVO;
import org.example.Vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LearnHistoryServiceImpl extends ServiceImpl<LearnHistoryMapper, LearnHistory> implements LearnHistoryService {

    @Autowired
    private LearnHistoryMapper learnHistoryMapper;

    @Resource
    private HttpUtil httpUtil;

    @Override
    public R getRecentSevenDayLearnData(String username) {
        Integer userId = httpUtil.getUserId(username);
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 获取最近7天的日期
        List<LocalDate> learnDates = new ArrayList<>();
        List<Integer> learnCounts = new ArrayList<>();
        for ( int i = 6 ; i >= 0 ; i-- ) {
            LocalDate localDate = today.minusDays(i);
            learnDates.add(localDate);
            LambdaQueryWrapper<LearnHistory> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(LearnHistory::getCreateTime,localDate);
            wrapper.eq(LearnHistory::getUserId,userId);
            List<LearnHistory> learnHistories = learnHistoryMapper.selectList(wrapper);
            if (learnHistories.isEmpty()){
                learnCounts.add(0);
            }else {
                learnCounts.add(learnHistories.size());
            }
        }
        return R.ok(new LearnHistoryLineVO(learnDates,learnCounts));
    }

    @Override
    public R getTodayLearnData(String username) {
        // 获取当前日期
        Integer userId = httpUtil.getUserId(username);
        LocalDate today = LocalDate.now();
        LambdaQueryWrapper<LearnHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LearnHistory::getCreateTime,today);
        wrapper.eq(LearnHistory::getUserId,userId);
        List<LearnHistory> learnHistories = learnHistoryMapper.selectList(wrapper);
        Map<String, Integer> strTypeMapper = new HashMap<>();
        Map<Integer,String> typeToStr = new HashMap<>();
        typeToStr.put(0,"安居乐业");
        typeToStr.put(1,"安士重迁");
        typeToStr.put(2,"授人以渔");
        typeToStr.put(3,"兵贵神速");
        typeToStr.put(4,"本末");
        for (LearnHistory learnHistory : learnHistories) {
            String strType = typeToStr.get(learnHistory.getType());
            strTypeMapper.put(strType,strTypeMapper.getOrDefault(strType,0)+1);
        }
        List<LearnHistoryPieVO> learnHistoryPieVOS = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : strTypeMapper.entrySet()) {
            learnHistoryPieVOS.add(new LearnHistoryPieVO(entry.getKey(),entry.getValue()));
        }
        return R.ok(learnHistoryPieVOS);
    }

    @Override
    public R addLearnHistory(LearnHistoryDTO learnHistoryDTO) {
        Integer userId = httpUtil.getUserId(learnHistoryDTO.getUsername());
        LearnHistory learnHistory = new LearnHistory();
        learnHistory.setWord(learnHistoryDTO.getWord());
        learnHistory.setType(learnHistoryDTO.getType());
        learnHistory.setUserId(userId);
        learnHistory.setCreateTime(LocalDate.now());
        learnHistoryMapper.insert(learnHistory);
        return R.ok();
    }
}
